package com.huike.web.controller.monitor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huike.clues.domain.SysOperLog;
import com.huike.clues.domain.dto.SysOperLogDTO;
import com.huike.clues.domain.dto.SysOperLogQueryDTO;
import com.huike.clues.service.ISysOperLogService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 操作日志记录
 *
 *
 */
@Api(tags = "日志")
@RequestMapping("/monitor/operlog")
@RestController
public class SysOperlogController extends BaseController {

    @Resource
    private ISysOperLogService iSysOperLogService;

    @ApiOperation("日志列表")
    @GetMapping("/list")
    public TableDataInfo getLogs(SysOperLogQueryDTO logDTO, HttpServletRequest request){
        if (!StringUtils.isEmpty(request.getParameter("params[beginTime]"))
        && !StringUtils.isEmpty(request.getParameter("params[endTime]"))){
            logDTO.setBeginCreateTime(request.getParameter("params[beginTime]"));
            logDTO.setEndCreateTime(request.getParameter("params[beginTime]"));
        }
        startPage();
        List<SysOperLog> data = iSysOperLogService.getLogs(logDTO);
        return getDataTable(data);
    }

    @ApiOperation("删除日志")
    @DeleteMapping("/{operIds}")
    public AjaxResult deleteLog(@PathVariable List<Long> operIds){
        iSysOperLogService.removeByIds(operIds);
        return AjaxResult.success();
    }

    @ApiOperation("清空日志")
    @DeleteMapping("/clean")
    public AjaxResult clean(){
        QueryWrapper<SysOperLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        iSysOperLogService.remove(queryWrapper);
        return AjaxResult.success();
    }

    @ApiOperation("操作日志导出")
    @GetMapping("/export")
    public AjaxResult<String> export(SysOperLogDTO operLog)
    {
        List<SysOperLogDTO> list = iSysOperLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLogDTO> util = new ExcelUtil<SysOperLogDTO>(SysOperLogDTO.class);
        return util.exportExcel(list, "操作日志");
    }
}
